Using Rigol Class to control oscilloscope


In [2]:
from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
import rigol
# initialize and set backend to usbtmc
r = rigol.Rigol("usbtmc")


1: Rigol Technologies - DS1000 SERIES 
Enter the number of the device you want to connect to: 1
Asking *IDN? returns: Rigol Technologies,DS1102E,DS1EB142004074,00.03.01.00.00

Query the waveform intensity

Let's take a look at some of the methods in the Rigol class

In [2]:
help(r)


Help on instance of Rigol in module rigol:

class Rigol
 |  Methods defined here:
 |  
 |  __init__(self, backend, idProduct=None, idVendor=None)
 |  
 |  acquireAverages(self, count)
 |      The commands set the average numbers in Average mode. <count>
 |      could be and integer of 2 times the power of N within 2 and 256.
 |  
 |  acquireMemDepth(self, depth)
 |      The commands set and query the memory depth of the oscilloscope. <depth>
 |      could be LONG (long memory) or NORMal (normal memory)
 |  
 |  acquireMode(self, mode)
 |      The commands set the current acquire mode of the oscilloscope.
 |      <mode> could be RTIM (Real time Sampling) or ETIM (Equivalent Sampling).
 |  
 |  acquireType(self, typ)
 |      The commands set the current acquire type of the oscilloscope.
 |      <type> could be NORM, AVER or PEAK.
 |  
 |  askAcquireAverages(self)
 |      The query returns 2, 4, 8, 16, 32, 64, 128 or 256.
 |  
 |  askAcquireMemDepth(self)
 |      The query returns LONG or NORMAL.
 |  
 |  askAcquireMode(self)
 |      Query acquire mode for oscilloscope.
 |      The query returns REAL_TIME or EQUAL_TIME.
 |  
 |  askAcquireSamplingRate(self, channel)
 |      The command queries the current sampling rate of the analog channel or digital
 |      channel (only for DS1000D series).
 |  
 |  askAcquireType(self)
 |      Query acquire type of oscilloscope.
 |      The query returns NORMAL, AVERAGE or PEAKDETECT
 |  
 |  askChannelBwlimit(self, channel)
 |      The query returns ON or OFF.
 |  
 |  askChannelCoupling(self, channel)
 |      The query returns AC, DC or GND.
 |  
 |  askChannelDisplay(self, channel)
 |      The query returns ON or OFF.
 |  
 |  askChannelFilter(self, channel)
 |      The query returns ON or OFF.
 |  
 |  askChannelInvert(self, channel)
 |      The query returns ON or OFF.
 |  
 |  askChannelMemoryDepth(self, channel)
 |      The command queries the memory depth of the specified channel. <n> could
 |      be 1 or 2.
 |      In long memory, up to 1Mpts could be stored in single channel and 512kpts in
 |      dual channels;
 |      In common memory, up to 16kpts could be stored in single channel and 8kpts in
 |      dual channels.
 |  
 |  askChannelOffset(self, channel)
 |      The query returns the setting value of <offset>.
 |  
 |  askChannelProbe(self, channel)
 |      The query returns the setting value of <attn>.
 |  
 |  askChannelScale(self, channel)
 |      query what the scale is for channel <channel>
 |  
 |  askChannelVernier(self, channel)
 |      The query returns Coarse or Fine.
 |  
 |  askDisplayBrightness(self)
 |      query the brightness of the grid
 |      returns a string from 0 to 32
 |  
 |  askDisplayGrid(self)
 |      The query returns FULL, HALF or NONE.
 |  
 |  askDisplayIntensity(self)
 |      Returns waveform brightness from 0 to 32
 |  
 |  askDisplayMnuDisplay(self)
 |      The query returns 1s, 2s, 5s, 10s, 20s or Infinite.
 |  
 |  askDisplayMnuStatus(self)
 |      The query returns ON or OFF.
 |  
 |  askDisplayPersist(self)
 |      The query returns ON or OFF.
 |  
 |  askDisplayType(self)
 |      The query returns VECTORS or DOTS.
 |  
 |  askKeyLock(self)
 |      The query returns ENABLE or DISABLE.
 |  
 |  askTeSensitivity(self)
 |      The query returns the setting value <count> in div.
 |  
 |  askTeSlope(self)
 |      The query returns POSITIVE or NEGATIVE.
 |  
 |  askTimebaseFormat(self)
 |      The query returns X-Y, Y-T or SCANNING.
 |  
 |  askTimebaseMode(self)
 |      The query returns MAIN or DELAYED.
 |  
 |  askTimebaseOffset(self, delayed=False)
 |      The query returns the setting value of the <offset> in s.
 |  
 |  askTimebaseScale(self, delayed=False)
 |      The query returns the setting value of <scale_val> in s.
 |  
 |  askTpMode(self)
 |      The query returns +GREATER THAN, +LESS THAN, +EQUAL, -GREATER THAN,
 |      -LESS THAN or -EQUAL.
 |  
 |  askTpSensitivity(self)
 |      The query returns the setting value of <count> in div.
 |  
 |  askTpWidth(self)
 |      The query returns the setting value of the <wid> in s.
 |  
 |  askTriggerCoupling(self, mode)
 |      The query returns DC, AC, HF or LF.
 |  
 |  askTriggerHoldoff(self)
 |      The query returns the setting value of <count> in s.
 |  
 |  askTriggerLevel(self, mode)
 |      The query returns the setting value of <level> in V.
 |  
 |  askTriggerMode(self)
 |      queries trigger mode
 |  
 |  askTriggerSource(self, mode)
 |      query what trigger source is being used.
 |      mode - can be any of ["EDGE", "PULS", "VIDEO", "SLOP", "PATT", "DUR", "ALT"]
 |  
 |  askTriggerStatus(self)
 |      The command queries the operating status of the oscilloscope. The status could
 |      be RUN, STOP, T`D, WAIT or AUTO.
 |  
 |  askTriggerSweep(self, mode)
 |      The query returns AUTO, NORMAL or SINGLE.
 |  
 |  askTsLevelA(self)
 |      The query returns the setting value of level in V.
 |  
 |  askTsLevelB(self)
 |      The query returns the setting value of level in V.
 |      Note: Level A (upper boundary) can not be less than the maximum of Level B
 |      (lower boundary).
 |  
 |  askTsMode(self)
 |      The query returns +GREATER THAN, +LESS THAN, +EQUAL, -GREATER THAN,
 |      -LESS THAN OR -EQUAL.
 |  
 |  askTsSensitivity(self)
 |      The query returns the setting value of <count> in div.
 |  
 |  askTsTime(self)
 |      The query returns the setting value of <count> in s.
 |  
 |  askTsWindow(self)
 |      The query returns P_WIN_A, P_WIN_B, P_WIN_AB, N_WIN_A, N_WIN_B or
 |      N_WIN_AB.
 |  
 |  askTvLine(self)
 |  
 |  askTvMode(self)
 |      The query returns ODD FIELD, EVEN FIELD, LINE or ALL LINES.
 |  
 |  askTvPolarity(self)
 |      The query returns POSITIVE or NEGATIVE.
 |  
 |  askTvSensitivity(self)
 |      The query returns the setting value of <count> in div.
 |  
 |  askTvStandard(self)
 |      The query returns NTSC or PAL/SECAM.
 |  
 |  askWaveformData(self, source)
 |      returns 1024 data for <source>.  Raw Data.
 |      run through numpy.frombuffer(data, "B") to get point data
 |      First 10 bytes are apparently a header, so we can skip those.
 |  
 |  askWaveformPointsMode(self)
 |      The query returns NORMal, MAXimum or RAW.
 |  
 |  auto(self)
 |  
 |  channelBwlimit(self, channel, on=True)
 |      The commands set and query the On/Off state of bandwidth limit. <n> could be
 |      1 or 2.
 |  
 |  channelCoupling(self, channel, coupling)
 |      The commands set and query the coupling mode of channel. DC indicates both
 |      the AC and DC components passed from input signal; AC indicates the blocked
 |      DC components; GND indicates to cut off the input of signal; <n> could be 1 or
 |      2.
 |  
 |  channelDisplay(self, channel, on=True)
 |      channel - either 1 or 2
 |      on - boolean value for turning the display on or off for the given channel.
 |  
 |  channelFilter(self, channel, on=True)
 |      The commands set and query the On/Off state of the filter. <n> could be 1 or 2.
 |  
 |  channelInvert(self, channel, on=True)
 |      The commands set and query the On/Off state of the waveform inverted. <n>
 |      could be 1 or 2.
 |  
 |  channelOffset(self, channel, offset)
 |      The commands set and query the vertical offset. <n> could be 1 or 2.
 |      When Scale≥250mV, the range of <offset>is -40V~+40V;
 |      When Scale<250mV, the range of <offset>is -2V~+2V.
 |  
 |  channelProbe(self, channel, attn)
 |      The commands set and query the attenuation factor of the probe. <n> could be
 |      1 or 2; <attn> could be 1, 5, 10, 50, 100, 500 or 1000.
 |  
 |  channelScale(self, channel, v)
 |      channel - Which channel to scale.
 |      v - voltage scale
 |      The commands set and query the vertical scale of waveform magnified by the
 |      oscilloscope. <n> could be 1 or 2.
 |      When the Probe is set to 1X, the range of <range> is 2mV ~ 10V;
 |      When the Probe is set to 5X, the range of <range> is 10mV ~50V;
 |      When the Probe is set to 10X, the range of <range> is 20mV ~ 100V;
 |      When the Probe is set to 50X, the range of <range> is 100mV ~ 500V;
 |      When the Probe is set to 100X, the range of <range> is 200mV ~ 1000V;
 |      When the Probe is set to 500X, the range of <range> is 1V ~5000V;
 |      When the Probe is set to 1000X, the range of <range> is 2V~ 10000V.
 |  
 |  channelVernier(self, channel, on=True)
 |      The commands set and query the adjusting mode of scale. ON denotes Fine,
 |      OFF denotes Coarse; <n> could be 1 or 2.
 |  
 |  convertVoltages(self, data, source)
 |      data - numpy array of unconverted voltage values.
 |  
 |  displayBrightness(self, level)
 |      Changes the brightness level of the grid.
 |      level - Brightness level from 0 to 32
 |  
 |  displayClear(self)
 |      The command clears out of date waveforms on the screen during waveform
 |      persist.
 |  
 |  displayGrid(self, grid)
 |      The commands set and query the state of the screen grid. <grid> could be FULL
 |      (open the background grid and coordinates), HALF (turn off the background grid)
 |      or NONE (turn off the background grid and coordinates).
 |  
 |  displayIntensity(self, level)
 |      level - intensity level from 0 to 32
 |  
 |  displayMnuDisplay(self, t)
 |      The commands set and query the time for hiding menus automatically. <time>
 |      could be 1s, 2s, 5s, 10s, 20s or Infinite.
 |  
 |  displayMnuStatus(self, disp=True)
 |      The commands set and query the state of the operation menu.
 |      disp can take boolean values True or False, to either display
 |          menu status or not.
 |  
 |  displayPersist(self, persist=True)
 |      The commands set and query the state of the waveform persist. “ON” denotes
 |      the record points hold until disable the presist, “OFF” denotes the record point
 |      varies in high refresh rate.
 |      persist can take on boolean values True or False.
 |  
 |  displayType(self, typ)
 |      The commands set the display type between sampling points. <type>
 |      could be VECT (vector display) or DOTS (point display).
 |  
 |  getWaveform(self, source)
 |      The data that is extracted from the oscilloscope needs to be inverted,
 |          multiplied by a constant and shifted by some voltage offset to get the
 |          true values.
 |      A custom method for retrieving the corrected voltage values out of the
 |          oscilloscope.
 |  
 |  hardcopy(self)
 |      Apparently saves a bitmap of the screen somewhere, but I have no idea where.
 |  
 |  identify(self)
 |  
 |  keyLock(self, enable=True)
 |      This command is used to enable/disable the buttons function on the front panel
 |      (except for “Force”).
 |  
 |  measureVpp(self, channel)
 |      channel is either 1 or 2
 |      returns peak to peak voltage in Volts.
 |  
 |  reset(self)
 |  
 |  run(self)
 |  
 |  stop(self)
 |      Stops data acquisition
 |  
 |  teSensitivity(self, count)
 |      The commands set and query the sensitive of edge trigger. The range of
 |      <count> could be 0.1div~1div.
 |  
 |  teSlope(self, positive=True)
 |      The commands set and query the type of edge trigger. The type could be
 |      POSitive (Rising edge) or NEGative (Failing edge).
 |  
 |  timebaseFormat(self, format)
 |      The commands set and query the horizontal timebase. <value> could be XY, YT
 |      or SCANning.
 |  
 |  timebaseMode(self, mode)
 |      The commands set and query the scan mode of horizontal timebase. <mode>
 |      could be MAIN (main timebase) or DELayed (delayed scan).
 |  
 |  timebaseOffset(self, offset, delayed=False)
 |      The commands set and query the offset of the MAIN or DELayed timebase (that
 |      is offset of the waveform position relative to the trigger midpoint.). Thereinto,
 |      In NORMAL mode, the range of <scale_val> is 1s ~ end of the memory;
 |      In STOP mode, the range of <scale_val> is -500s ~ +500s;
 |      In SCAN mode, the range of <scale_val> is -6*Scale ~ +6*Scale; (Note: Scale
 |      indicates the current horizontal scale, the unit is s/div.)
 |      In MAIN state, the item [:DELayed] should be omitted.
 |  
 |  timebaseScale(self, scale, delayed=False)
 |      The commands set and query the horizontal scale for MAIN or DELayed
 |      timebase, the unit is s/div (seconds/grid), thereinto:
 |      In YT mode, the range of <scale_val> is 2ns - 50s;
 |      In ROLL mode, the range of <scale_val> is 500ms - 50s;
 |      In MAIN state, the item [:DELayed] should be omitted.
 |  
 |  tpMode(self, mode)
 |      The commands set and query the pulse condition. <mode> could be
 |      +GREaterthan (positive pulse greater than), +LESSthan (positive pulse less
 |      than), +EQUal (positive pulse equals to), -GREaterthan (negative pulse greater
 |      than), -LESSthan (negative pulse less than) or –EQUal (negative pulse equals
 |      to).
 |  
 |  tpSensitivity(self, count)
 |      The commands set and query the sensitive of pulse trigger. The range of
 |      <count> could be 0.1div~1div.
 |  
 |  tpWidth(self, width)
 |      The commands set and query the pulse width. The range of <wid> is 20ns ~
 |      10s.
 |  
 |  trigger50(self)
 |      The command sets the trigger level to the vertical midpoint of amplitude.
 |  
 |  triggerCoupling(self, mode, coupling)
 |      The commands set and query the coupling type. Thereinto,
 |      DC: Allow all signals pass.
 |      AC: Block DC signals and attenuate the signals lower than 10Hz.
 |      HF: Reject high frequency signals (Higher than 150KHz).
 |      LF: Reject DC signals and attenuate low frequency signals (Lower than 8KHz).
 |      <mode> could be :EDGE, :PULSe or :SLOPe.
 |  
 |  triggerForce(self)
 |      The command forces the oscilloscope to trigger signal, which is usually used in
 |      “Normal” and “Single” mode.
 |  
 |  triggerHoldoff(self, count)
 |      The commands set and query the trigger holfoff time. The range of <count> is
 |      <count>: 500ns~1.5s.
 |  
 |  triggerLevel(self, mode, level)
 |      The commands set and query the trigger level. <mode> could be :EDGE, :PULSe
 |      or :VIDEO; the range of <level> is: -6*Scale~+6*Scale, Scale indicates the current vertical
 |      scale, the unit is V/div.
 |  
 |  triggerMode(self, mode)
 |      sets trigger mode
 |      see valid_modes variable for valid mode values.
 |  
 |  triggerSource(self, mode, source)
 |      sets trigger mode and source
 |  
 |  triggerSweep(self, mode, sweep)
 |      The commands set and query the trigger type. <mode>could be :EDGE,
 |      :PULSe, :SLOPe, :PATTern or :DURation.
 |      sweep can take values [AUTO, NORM, SING]
 |  
 |  tsLevelA(self, value)
 |      The commands set and query the upper boundary “Level A” of trigger level. The
 |      range of <value> is LevelB~+ 6*Scale; Scale indicates the current vertical level,
 |      the unit is V/div.
 |  
 |  tsLevelB(self, value)
 |      The commands set and query the lower boundary “LEVel B” of trigger level. The
 |      range of <value> is -6*Scale~LevelA; Scale indicates the current vertical level,
 |      the unit is V/div.
 |  
 |  tsMode(self, mode)
 |      The commands set and query the slope condition. <mode> could be
 |      +GREaterthan (positive slope greater than), +LESSthan (positive slope less
 |      than), + EQUal (positive slope equals to), -GREaterthan (negative slope greater
 |      than), -LESSthan (negative slope less than) or –EQUal (negative slope equals
 |      to).
 |  
 |  tsSensitivity(self, count)
 |      The commands set and query the trigger sensitive. The range of <count> is:
 |      0.1div ~1div.
 |  
 |  tsTime(self, count)
 |      The commands set and query the time setting about slope trigger. The range of
 |      <count> is 20ns~10s.
 |  
 |  tsWindow(self, count)
 |      The commands set and query the type of trigger level which can be adjusted by
 |      the level knob on the oscilloscope.
 |      When the slope condition is +GREaterthan, +LESSthan or + EQUal, <count>
 |      could be PA (rising edge Level A), PB (rising edge Level B) or PAB (rising edge
 |      Level AB);
 |      When the slope condition is -GREaterthan, -LESSthan or –EQUal, <count> could
 |      be NA (falling edge Level A), NB (falling edge LevelB) or NAB (falling edge
 |      LevelAB).
 |  
 |  tvLine(self, value)
 |      The commands set and query the number of specified line of synchronous. In
 |      NTSC standard, the range of <value> is 1~525; in PAL/SECAM standard, the
 |      range of <value> is 1~625.
 |  
 |  tvMode(self, mode)
 |      The commands set and query the synchronous mode of the video trigger.
 |      <mode> could be ODDfield, EVENfield, LINE or ALLlines.
 |  
 |  tvPolarity(self, pos_polarity=True)
 |      The commands set and query the video polarity. The polarity could be POSitive
 |      or NEGative.
 |  
 |  tvSensitivity(self, count)
 |      The commands set and query the trigger sensitive, the range of <count> is:
 |      0.1div ~1div.
 |  
 |  tvStandard(self, ntsc=True)
 |      The commands set and query the type of video trigger standard.
 |  
 |  waveformPointsMode(self, mode)
 |      This command sets the mode of waveform points. <points_mode> can be:
 |      NORMal, MAXimum or RAW.
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  backends = ['usbtmc']

Do note that I'm writing all of these methods so that the oscilloscope can be controlled without looking at the programmers manual at all, but you can always write in your own commands (in case I have not implemented them) by using the Rigol.dev.write() or Rigol.dev.ask() methods.

Basic Methods


In [3]:
r.stop()

In [2]:
r.auto()

In [5]:
r.run()

Display Methods


In [6]:
r.askIntensity()


Out[6]:
u'32'

Set intensity to 32.


In [7]:
r.intensity(32)

Trigger Methods

Set trigger source for CHAN1 to EDGE


In [8]:
r.triggerSource("EDGE", "CHAN1")

Query trigger source for edge


In [9]:
r.askTriggerSource("EDGE")


Out[9]:
u'CH1'

Channel Methods

Turn on channel 2 and then turn it off


In [3]:
r.channelDisplay(2)

In [4]:
r.channelDisplay(2, on=False)

Turn on channel 1


In [5]:
r.channelDisplay(1)

Set channel scale


In [28]:
r.channelScale(1, 1)

In [29]:
r.askChannelScale(1)


Out[29]:
u'1.000e+00'

You can also access the volt1_scale attribute (Updated everytime you send the channelScale() method)


In [30]:
r.volt1_scale


Out[30]:
u'1.000e+00'

Measure Methods

Measure peak to peak voltage


In [7]:
r.measureVpp(1)


Out[7]:
u'6.24e+00'

Key Methods

Unlock the buttons on the oscilloscope Note: The next time you send a usb command or query, the keys will relock


In [16]:
r.keyLock(enable=False)

TimeBase Methods

Set a time scale in sec/division


In [25]:
r.timebaseScale(5e-3)

Ask for the time scale in sec/division


In [26]:
r.askTimebaseScale()


Out[26]:
u'5.000e-03'

There is also an attribute you can look up without querying the oscilloscope. (Potentially faster) This attribute will update every time you send the timebaseScale() method to the oscilloscope.


In [27]:
r.time_scale


Out[27]:
u'5.000e-03'

Waveform Methods

plot something


In [3]:
% matplotlib inline

In [42]:
r.run()

In [4]:
r.waveformPointsMode("NORM")

In [12]:
x = r.getTimebase()

In [13]:
y = r.getWaveform("CHAN1")

In [59]:
plt.plot(x, y)
plt.xlabel("TIME (SECONDS)")
plt.ylabel("VOLTAGE (V)")
plt.title("Oscilloscope Reading")


Out[59]:
<matplotlib.text.Text at 0x7fab9c1edf90>